﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Transactions;
using PayrollSystemLibrary;
using PayrollSystemLibrary.Model;
using System.Linq;

namespace IntegrationTestPayrollLibrary.AddEmployee
{
    [TestClass]
    public class GivenEmployeeNotInDatabase
    {
        private TransactionScope _trans;
        [TestInitialize]
        public void Setup()
        {
            _trans= new TransactionScope();
        }
        [TestCleanup]
        public void CleanUp()
        {
            if (_trans!=null) _trans.Dispose();
        }

        [TestMethod]
        public void WhenAddingValidEmployee_ThenEmployeeIsPersisted()
        {
            Employee emp = CreateValidHourlyEmp();
            
                InsertIntoDatabase(emp);
                Employee dbEmp = GetEmpFromDatabase(emp.EmpId);
                Assert.IsNotNull(dbEmp);
                Assert.AreEqual(emp.Name, dbEmp.Name);
           
        }

        private Employee GetEmpFromDatabase(int empid)
        {
            Employee dbEmp;
            using (var db2 = new DataAccessHelper())
            {
                dbEmp = db2.FindByEmployeeId(empid);
            }
            return dbEmp;
        }

        private void InsertIntoDatabase(Employee emp)
        {
            using (var db = new DataAccessHelper())
            {
                db.Add((emp));
                db.Save();
            }
        }

        private Employee CreateValidHourlyEmp()
        {
            var emp = new Employee();
            emp.Adress = "dummy";
            emp.BankAccount = "dummy";
            emp.BankName = "dummy";
            emp.Classification = "H";
            emp.EmpId = 1;
            emp.HourlyRate = 8;
            emp.IsAffiliated = false;
            emp.Name = "dummy";
            emp.PaymentMethod = "H";
            emp.PaymentSchedule = "W";
            return emp;
        }
    }
}
